package com.hrm.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.hrm.dao.EmpAttendanceMapper;
import com.hrm.dao.SysEventMapper;
import com.hrm.dto.EmpAttendanceDTO;
import com.hrm.entity.EmpAttendance;
import com.hrm.entity.Employee;
import com.hrm.entity.SysEvent;
import com.hrm.service.AttendanceService;
import com.hrm.utils.SysEventDescription;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Auther: zy
 * @Date: 2020/5/28 17:45
 * @Description:
 */
@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    EmpAttendanceMapper empAttendanceMapper;
    @Autowired
    SysEventMapper sysEventMapper;

    @Override
    public String empAttendance(HttpSession session) {
        Employee employee = (Employee) session.getAttribute("user");
        EmpAttendance empAttendance = new EmpAttendance();
        if(empAttendanceMapper.selectTodayByEmpId(employee.getId()) != null ) {
            return "打卡失败,您已签到";
        }else {
            empAttendance.setEmpId(employee.getId());
            String nowHour = DateUtil.format(DateUtil.date(),"HH:mm:ss");
            String nowDay = DateUtil.format(DateUtil.date(),"yyyy-MM-dd");
            Date nowDate = DateUtil.parse(nowHour);
            Date ruleDate = DateUtil.parse("08:20:00");
            Long between = DateUtil.between(nowDate,ruleDate, DateUnit.MINUTE);
            empAttendance.setArriveTime(DateUtil.date());
            empAttendance.setDatetime(DateUtil.parse(nowDay));
            if(between<=20&&between>0){
                empAttendance.setAttendanceType("迟到");
                empAttendanceMapper.insert(empAttendance);
                String description = SysEventDescription.addSysEventDescription(employee.getId(),employee.getName(),"员工打卡");
                SysEvent sysEvent = SysEventDescription.addSysEvent(DateUtil.date(),description);
                sysEventMapper.insert(sysEvent);
                return "打卡成功,您已迟到";
            }else if(between>20){
                empAttendance.setAttendanceType("缺勤");
                empAttendanceMapper.insert(empAttendance);
                return "打卡失败,您已缺勤";
            }else {
                empAttendance.setAttendanceType("到勤");
                empAttendanceMapper.insert(empAttendance);
                return "打卡成功,您已到勤";
            }
        }
    }

    @Override
    public EmpAttendanceDTO empStatistical(Integer empId,Integer deptId) {
            EmpAttendanceDTO empAttendanceDTO = empAttendanceMapper.selecetMonthByEmpId(empId,deptId);
            if(empAttendanceDTO!=null){
                empAttendanceDTO.setAttendanceTimes(empAttendanceMapper.empAttendance(empId,deptId));
                empAttendanceDTO.setAbsenceTimes(empAttendanceMapper.empAbsence(empId,deptId));
                empAttendanceDTO.setLateTimes(empAttendanceMapper.empLate(empId,deptId));
                return empAttendanceDTO;
            }else return null;

    }
}
